d2 <- data.frame(id = 20000 + as.numeric(rownames(d.yg)))

# Treatment assignment and compliance
d2$assigned.to.info.only <- 1 * (d.yg$randtrt == 2)
d2$assigned.to.session <- 1 * (d.yg$randtrt == 1)
d2$attended.session <- 1 * (d.yg$participation %in% 1:2)
.ok <- d2$assigned.to.info.only == 0 & d2$assigned.to.session == 0
d2$assigned.to.session[.ok] <- d2$attended.session[.ok] <- NA

# Demographics
d2$Female <- 1 * (d.yg$gender == 2)
d2$Age <- 2008 - d.yg$birthyr
d2$Education <- d.yg$educ
d2$Education[d.yg$educ == 4] <- 3
d2$Education[d.yg$educ %in% 5:6] <- 4
d2$White <- 1 *(d.yg$race == 1)
d2$Black  <- 1 *(d.yg$race == 2)
d2$Latino <- 1 *(d.yg$race == 3)
d2$Other <- 1 *(d.yg$race %in% 4:7)
d2$Income <- NA
d2$Income[d.yg$income == 1] <- 1 # less than $10k
d2$Income[d.yg$income == 2] <- 2 # $10k to $15k
d2$Income[d.yg$income == 3] <- 3 # $15k to $20k
d2$Income[d.yg$income == 4] <- 4 # $20k to $25k
d2$Income[d.yg$income == 5] <- 5 # $25k to $30k
d2$Income[d.yg$income == 6] <- 6 # $30k to $40k
d2$Income[d.yg$income == 7] <- 7 # $40k to $50k
d2$Income[d.yg$income == 8] <- 8 # $50k to $60k
d2$Income[d.yg$income %in% 9:11] <- 9 # $60k to $100k
d2$Income[d.yg$income %in% 12:13] <- 10 # $100k to $150k
d2$Income[d.yg$income == 14] <- 11 # $150k or more
d2$Party7pt <- NA
d2$Party7pt[d.yg$pid7 == 1] <- 7
d2$Party7pt[d.yg$pid7 == 2] <- 6
d2$Party7pt[d.yg$pid7 == 3] <- 5
d2$Party7pt[d.yg$pid7 == 4] <- 4
d2$Party7pt[d.yg$pid7 == 5] <- 3
d2$Party7pt[d.yg$pid7 == 6] <- 2
d2$Party7pt[d.yg$pid7 == 7] <- 1
d2$same.party <- 1 * (d.yg$pid7 %in% 1:3)

# Baseline agreement on whether approve of torture
d2$torture.baseline <- d.yg$q36 - 1

# Followup agreement on Waterboarding & Guantanamo
d2$waterboarding.followup <- d.yg$Q34.post - 1
d2$guantanamo.followup <- d.yg$Q37.post - 1

# Trust Levin
d2$trust.baseline <- 3 - (d.yg$q11 - 1)
d2$trust.followup <- 3 - (d.yg$Q17.post - 1)

# Approval of Levin
d2$approval.baseline <- 4 - (d.yg$q7 - 1)
d2$approval.followup <- 4 - (d.yg$Q12c.post - 1)

# Intent to Vote for Levin
d2$vote.baseline <- 4 - (d.yg$q9 - 1)
d2$vote.baseline[d.yg$q9 %in% 6:8] <- 0
d2$vote.followup <- 4 - (d.yg$Q21.post - 1)
d2$vote.followup[d.yg$Q21.post %in% 6:8] <- 0

# Actual vote
d2$vote.november <- as.vector(1 * (d.yg$Nov.v5 == 1))
d2$turnout.november <- as.vector(1 * (d.yg$Nov.v3 == 5))

# Drop 'true controls' who were not assigned to either attend session or receive information
d2 <- subset(d2, !is.na(attended.session))

# recode baseline on 0-1 scale (to estimate Null SDs for Cohen's d's)
d2$torture.baseline.01 <- scale01(d2$torture.baseline)
d2$trust.baseline.01 <- scale01(d2$trust.baseline)
d2$approval.baseline.01 <- scale01(d2$approval.baseline)
d2$vote.baseline.01 <- scale01(d2$vote.baseline)

# put responses on 0-1 scale
d2$waterboarding.followup.01 <- scale01(d2$waterboarding.followup)
d2$guantanamo.followup.01 <- scale01(d2$guantanamo.followup)
d2$trust.followup.01 <- scale01(d2$trust.followup)
d2$approval.followup.01 <- scale01(d2$approval.followup)
d2$vote.followup.01 <- scale01(d2$vote.followup)
d2$vote.november.01 <- scale01(d2$vote.november)
d2$turnout.november.01 <- scale01(d2$turnout.november)

# recode baseline to include missingness, then create factor
d2$torture.baseline.NArecode <- NArecode(d2$torture.baseline)
d2$trust.baseline.NArecode <- NArecode(d2$trust.baseline)
d2$approval.baseline.NArecode <- NArecode(d2$approval.baseline)
d2$vote.baseline.NArecode <- NArecode(d2$vote.baseline)

# Calculate inverse probability of response weights
prob <- glm(is.na(waterboarding.followup) ~ 
            torture.baseline.NArecode + assigned.to.session,
            data = d2, family = binomial)$fitted.values
d2$ipw.waterboarding.followup <- 1 / prob
prob <- glm(is.na(guantanamo.followup) ~ 
            torture.baseline.NArecode + assigned.to.session, 
            data = d2, family = binomial)$fitted.values
d2$ipw.guantanamo.followup <- 1 / prob
prob <- glm(is.na(trust.followup) ~ 
            trust.baseline.NArecode + assigned.to.session,
            data = d2, family = binomial)$fitted.values
d2$ipw.trust.followup <- 1 / prob
prob <- glm(is.na(approval.followup) ~ 
            approval.baseline.NArecode + assigned.to.session,
            data = d2, family = binomial)$fitted.values
d2$ipw.approval.followup <- 1 / prob
prob <- glm(is.na(vote.followup) ~ 
            vote.baseline.NArecode + assigned.to.session,
            data = d2, family = binomial)$fitted.values
d2$ipw.vote.followup <- 1 / prob
prob <- glm(is.na(vote.november) ~ 
            vote.baseline.NArecode + assigned.to.session,
            data = d2, family = binomial)$fitted.values
d2$ipw.vote.november <- 1 / prob